package rabbitmq.wrok;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 工作队列
 */

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("110.41.84.205");
        factory.setPort(5672);
        factory.setUsername("study");
        factory.setPassword("study");
        factory.setVirtualHost("Demo");
        // 建立连接
        Connection connection = factory.newConnection();
        // 建立通信
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare(Constants.WORK_QUEUE_MAME, true,false,false,null);
        // 发送消息
        for(int i = 0; i < 10; i++) {
            String msg = "hello word " + i;
            channel.basicPublish("",Constants.WORK_QUEUE_MAME,null,msg.getBytes());
        }
        System.out.println("发送成功");
        // 释放资源
        channel.close();
        connection.close();
    }
}
